/**
 * Filename: SettingActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:36
 */
package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.umeng.analytics.MobclickAgent;

/**
 * 设置界面
 */
public class MMLockActivity extends BaseActivity implements View.OnClickListener, ISkinUpdate {
    private Toolbar mToolbar;
    private RelativeLayout setting_codenum;
    private ToggleButton numSwitch;
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_mmlock);
        registerBoradcastReceiver();
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void initView() {
        mSharedPreferences = getSharedPreferences(ACTIONs.mypassword, 0);
        mToolbar = (Toolbar) findViewById(R.id.setting_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        setting_codenum = (RelativeLayout) findViewById(R.id.setting_codenum);
        numSwitch = (ToggleButton) findViewById(R.id.setting_switch1);
        setting_codenum.setOnClickListener(this);
        SetSwitch();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionSetNumCode);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionSetNumCode:
                    SetSwitch();
                    break;
            }
        }
    };

    private void SetSwitch() {
        boolean bb = mSharedPreferences.getBoolean(ACTIONs.numcodepasswordbb, false);
        if (bb) {
            numSwitch.setChecked(true);
        } else {
            numSwitch.setChecked(false);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_codenum:
                String passwordmm = mSharedPreferences.getString(ACTIONs.numcodepasswordmm, "");
                boolean bb = mSharedPreferences.getBoolean(ACTIONs.numcodepasswordbb, false);
                if (!bb) {
                    if (passwordmm == null || passwordmm.equals("")) {
                        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
                        builder.setCancelable(false);
                        final android.support.v7.app.AlertDialog dlg = builder.create();
                        dlg.show();
                        Window window = dlg.getWindow();
                        window.setContentView(R.layout.pub_dialog);
                        window.setBackgroundDrawableResource(R.color.transparent);
                        WindowManager.LayoutParams layout = dlg.getWindow().getAttributes();
                        layout.width = DisplayUtils.getScreenWidthPixels(this) - DisplayUtils.dp2px(this, 35 * 2);
                        dlg.getWindow().setAttributes(layout);
                        TextView tt = (TextView) window.findViewById(R.id.pp_title);
                        tt.setText("提示");
                        TextView content = (TextView) window.findViewById(R.id.pp_content);
                        content.setText("请牢记密码，否则你将无法再次进入忆年");
                        TextView cancel = (TextView) window
                                .findViewById(R.id.btn_cancel);
                        cancel.setText("取消");
                        cancel.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                numSwitch.setChecked(false);
                                dlg.cancel();
                            }
                        });
                        final TextView ok = (TextView) window.findViewById(R.id.btn_ok);
                        ok.setText("去设置密码");
                        ok.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                intent2Activity(SetNumCodeMM.class);
                                dlg.cancel();
                            }
                        });
                    } else {
                        intent2Activity(SetNumCodeMM.class);
                    }
                } else {
                    if (numSwitch.isChecked()) {
                        intent2Activity(SetNumCodeMMCancel.class);
                    }
                }
                break;
        }
    }


    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
        unregisterReceiver(mBroadcastReceiver);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
